<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>anim.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_fx_anim_anim.js.html">anim.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Basic animation controls.
<a name="line17"></a> *
<a name="line18"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line19"></a> */
<a name="line20"></a>goog.provide(&#39;goog.fx.anim&#39;);
<a name="line21"></a>goog.provide(&#39;goog.fx.anim.Animated&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.async.AnimationDelay&#39;);
<a name="line24"></a>goog.require(&#39;goog.async.Delay&#39;);
<a name="line25"></a>goog.require(&#39;goog.object&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * An interface for programatically animated objects. I.e. rendered in
<a name="line31"></a> * javascript frame by frame.
<a name="line32"></a> *
<a name="line33"></a> * @interface
<a name="line34"></a> */
<a name="line35"></a>goog.fx.anim.Animated = function() {};
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Function called when a frame is requested for the animation.
<a name="line40"></a> *
<a name="line41"></a> * @param {number} now Current time in milliseconds.
<a name="line42"></a> */
<a name="line43"></a>goog.fx.anim.Animated.prototype.onAnimationFrame;
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * Default wait timeout for animations (in milliseconds).  Only used for timed
<a name="line48"></a> * animation, which uses a timer (setTimeout) to schedule animation.
<a name="line49"></a> *
<a name="line50"></a> * @type {number}
<a name="line51"></a> * @const
<a name="line52"></a> */
<a name="line53"></a>goog.fx.anim.TIMEOUT = goog.async.AnimationDelay.TIMEOUT;
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * A map of animations which should be cycled on the global timer.
<a name="line58"></a> *
<a name="line59"></a> * @type {Object.&lt;number, goog.fx.anim.Animated&gt;}
<a name="line60"></a> * @private
<a name="line61"></a> */
<a name="line62"></a>goog.fx.anim.activeAnimations_ = {};
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * An optional animation window.
<a name="line67"></a> * @type {Window}
<a name="line68"></a> * @private
<a name="line69"></a> */
<a name="line70"></a>goog.fx.anim.animationWindow_ = null;
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * An interval ID for the global timer or event handler uid.
<a name="line75"></a> * @type {goog.async.Delay|goog.async.AnimationDelay}
<a name="line76"></a> * @private
<a name="line77"></a> */
<a name="line78"></a>goog.fx.anim.animationDelay_ = null;
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Registers an animation to be cycled on the global timer.
<a name="line83"></a> * @param {goog.fx.anim.Animated} animation The animation to register.
<a name="line84"></a> */
<a name="line85"></a>goog.fx.anim.registerAnimation = function(animation) {
<a name="line86"></a>  var uid = goog.getUid(animation);
<a name="line87"></a>  if (!(uid in goog.fx.anim.activeAnimations_)) {
<a name="line88"></a>    goog.fx.anim.activeAnimations_[uid] = animation;
<a name="line89"></a>  }
<a name="line90"></a>
<a name="line91"></a>  // If the timer is not already started, start it now.
<a name="line92"></a>  goog.fx.anim.requestAnimationFrame_();
<a name="line93"></a>};
<a name="line94"></a>
<a name="line95"></a>
<a name="line96"></a>/**
<a name="line97"></a> * Removes an animation from the list of animations which are cycled on the
<a name="line98"></a> * global timer.
<a name="line99"></a> * @param {goog.fx.anim.Animated} animation The animation to unregister.
<a name="line100"></a> */
<a name="line101"></a>goog.fx.anim.unregisterAnimation = function(animation) {
<a name="line102"></a>  var uid = goog.getUid(animation);
<a name="line103"></a>  delete goog.fx.anim.activeAnimations_[uid];
<a name="line104"></a>
<a name="line105"></a>  // If a timer is running and we no longer have any active timers we stop the
<a name="line106"></a>  // timers.
<a name="line107"></a>  if (goog.object.isEmpty(goog.fx.anim.activeAnimations_)) {
<a name="line108"></a>    goog.fx.anim.cancelAnimationFrame_();
<a name="line109"></a>  }
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Tears down this module. Useful for testing.
<a name="line115"></a> */
<a name="line116"></a>// TODO(nicksantos): Wow, this api is pretty broken. This should be fixed.
<a name="line117"></a>goog.fx.anim.tearDown = function() {
<a name="line118"></a>  goog.fx.anim.animationWindow_ = null;
<a name="line119"></a>  goog.dispose(goog.fx.anim.animationDelay_);
<a name="line120"></a>  goog.fx.anim.animationDelay_ = null;
<a name="line121"></a>  goog.fx.anim.activeAnimations_ = {};
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Registers an animation window. This allows usage of the timing control API
<a name="line127"></a> * for animations. Note that this window must be visible, as non-visible
<a name="line128"></a> * windows can potentially stop animating. This window does not necessarily
<a name="line129"></a> * need to be the window inside which animation occurs, but must remain visible.
<a name="line130"></a> * See: https://developer.mozilla.org/en/DOM/window.mozRequestAnimationFrame.
<a name="line131"></a> *
<a name="line132"></a> * @param {Window} animationWindow The window in which to animate elements.
<a name="line133"></a> */
<a name="line134"></a>goog.fx.anim.setAnimationWindow = function(animationWindow) {
<a name="line135"></a>  // If a timer is currently running, reset it and restart with new functions
<a name="line136"></a>  // after a timeout. This is to avoid mismatching timer UIDs if we change the
<a name="line137"></a>  // animation window during a running animation.
<a name="line138"></a>  //
<a name="line139"></a>  // In practice this cannot happen before some animation window and timer
<a name="line140"></a>  // control functions has already been set.
<a name="line141"></a>  var hasTimer =
<a name="line142"></a>      goog.fx.anim.animationDelay_ &amp;&amp; goog.fx.anim.animationDelay_.isActive();
<a name="line143"></a>
<a name="line144"></a>  goog.dispose(goog.fx.anim.animationDelay_);
<a name="line145"></a>  goog.fx.anim.animationDelay_ = null;
<a name="line146"></a>  goog.fx.anim.animationWindow_ = animationWindow;
<a name="line147"></a>
<a name="line148"></a>  // If the timer was running, start it again.
<a name="line149"></a>  if (hasTimer) {
<a name="line150"></a>    goog.fx.anim.requestAnimationFrame_();
<a name="line151"></a>  }
<a name="line152"></a>};
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * Requests an animation frame based on the requestAnimationFrame and
<a name="line157"></a> * cancelRequestAnimationFrame function pair.
<a name="line158"></a> * @private
<a name="line159"></a> */
<a name="line160"></a>goog.fx.anim.requestAnimationFrame_ = function() {
<a name="line161"></a>  if (!goog.fx.anim.animationDelay_) {
<a name="line162"></a>    // We cannot guarantee that the global window will be one that fires
<a name="line163"></a>    // requestAnimationFrame events (consider off-screen chrome extension
<a name="line164"></a>    // windows). Default to use goog.async.Delay, unless
<a name="line165"></a>    // the client has explicitly set an animation window.
<a name="line166"></a>    if (goog.fx.anim.animationWindow_) {
<a name="line167"></a>      // requestAnimationFrame will call cycleAnimations_ with the current
<a name="line168"></a>      // time in ms, as returned from goog.now().
<a name="line169"></a>      goog.fx.anim.animationDelay_ = new goog.async.AnimationDelay(
<a name="line170"></a>          function(now) {
<a name="line171"></a>            goog.fx.anim.cycleAnimations_(now);
<a name="line172"></a>          }, goog.fx.anim.animationWindow_);
<a name="line173"></a>    } else {
<a name="line174"></a>      goog.fx.anim.animationDelay_ = new goog.async.Delay(function() {
<a name="line175"></a>        goog.fx.anim.cycleAnimations_(goog.now());
<a name="line176"></a>      }, goog.fx.anim.TIMEOUT);
<a name="line177"></a>    }
<a name="line178"></a>  }
<a name="line179"></a>
<a name="line180"></a>  var delay = goog.fx.anim.animationDelay_;
<a name="line181"></a>  if (!delay.isActive()) {
<a name="line182"></a>    delay.start();
<a name="line183"></a>  }
<a name="line184"></a>};
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * Cancels an animation frame created by requestAnimationFrame_().
<a name="line189"></a> * @private
<a name="line190"></a> */
<a name="line191"></a>goog.fx.anim.cancelAnimationFrame_ = function() {
<a name="line192"></a>  if (goog.fx.anim.animationDelay_) {
<a name="line193"></a>    goog.fx.anim.animationDelay_.stop();
<a name="line194"></a>  }
<a name="line195"></a>};
<a name="line196"></a>
<a name="line197"></a>
<a name="line198"></a>/**
<a name="line199"></a> * Cycles through all registered animations.
<a name="line200"></a> * @param {number} now Current time in milliseconds.
<a name="line201"></a> * @private
<a name="line202"></a> */
<a name="line203"></a>goog.fx.anim.cycleAnimations_ = function(now) {
<a name="line204"></a>  goog.object.forEach(goog.fx.anim.activeAnimations_, function(anim) {
<a name="line205"></a>    anim.onAnimationFrame(now);
<a name="line206"></a>  });
<a name="line207"></a>
<a name="line208"></a>  if (!goog.object.isEmpty(goog.fx.anim.activeAnimations_)) {
<a name="line209"></a>    goog.fx.anim.requestAnimationFrame_();
<a name="line210"></a>  }
<a name="line211"></a>};
</pre>


</body>
</html>
